
extends Node2D

# member variables here, example:
# var a=2
# var b="textvar"

var speed=10
var rate=6
var timer
var emitSpeed=.1
var isDown=false
var onCollision
var screen
var life=3
func _ready():
	timer=get_node("Timer")
	timer.set_wait_time(emitSpeed)
	var rect=get_viewport_rect()
	set_pos(Vector2(rect.size.width/2,rect.size.height-200))
	set_process_input(true)
	screen=get_viewport_rect()
	set_process(true)
	pass
	
func _input(event):
	if life>0:
		if Input.is_action_pressed("shoot"):
			if isDown==false:
				isDown=true
				timer.stop()
				timer.start()
				#_on_Timer_timeout()
		else:
			isDown=false
			timer.stop()
		
		

		
		
func _process(delta):
	if life>0:
		var pos=get_pos()
		if Input.is_action_pressed("ui_left"):
			pos.x -= speed
		if Input.is_action_pressed("ui_right"):
			pos.x += speed
	
			
		if Input.is_action_pressed("ui_up"):
			pos.y -= speed
	
			
			
		if Input.is_action_pressed("ui_down"):
			pos.y += speed
			
		
		if pos.x<=0 :
			pos.x=0
		if pos.x>screen.size.width:
			pos.x=screen.size.width
		if pos.y<=0 :
			pos.y=0
		if pos.y>=screen.size.height:
			pos.y=screen.size.height
		set_pos(pos)

func _on_Timer_timeout():
	if life>0:
		get_node("sound").play("shoot")
		var bullet=preload("res://hero/bullet.scn").instance()
		var pos=get_pos()
		pos.y-=70
		bullet.set_pos(pos)
		get_tree().get_current_scene().add_child(bullet)



func _on_Area2D_area_enter_shape( area_id, area, area_shape, area_shape ):
	if area.get_name()=="enemy_area":
		print(22)
		get_tree().get_current_scene().get_node("shake").play("shake——_bg",-1,3)
		get_node("AnimationPlayer").play("attacked",-1,.2)
		life-=1
		if life>=0:
			var lifestr=""
			for i in range(0,life):
				lifestr+="I"
			get_tree().get_current_scene().get_node("hero_life").set_text(lifestr)
			if life==0:
				remove_child(get_node("Area2D"))
				get_node("AnimationPlayer").play("explode",-1,.1)


func _on_AnimationPlayer_finished():
	if life<=0:
		var root=get_tree().get_current_scene()
		root.get_node("enemyEmitter").stop()
		root.get_node("bg").speed=1
		var panel=preload("res://score/score.scn").instance()
		root.add_child(panel)
		panel.get_node("time").set_text("time:"+str(int(root.time*10)/10))
		panel.get_node("score").set_text("score:"+str(root.score))
		queue_free()
	else:
		get_node("AnimationPlayer").play("play");
